from PySide6.QtCore import QDateTime, Qt, Slot
from PySide6.QtWidgets import QDialog

 # 导入我们生成的界面
from Forms.MessagesWindow_UI import Ui_FormMessages

class MessagesWindow(QDialog, Ui_FormMessages):
    DefaultColor = Qt.black
    ChangeColor = Qt.blue

    def __init__(self, parent = None):
        super().__init__()
        self.setupUi(self)

        flags = self.windowFlags()
        self.setWindowFlags(flags & ~Qt.WindowContextHelpButtonHint)
        
        self.__sentColor = MessagesWindow.ChangeColor
        self.__recvColor = MessagesWindow.ChangeColor

    def SetDeviceIP(self, ip):
        self.setWindowTitle("历史消息" + (":" + ip if len(ip) > 0 else ""))

    def AddSentMsg(self, sentMsg):
        self.__sentColor = MessagesWindow.DefaultColor if self.__sentColor == MessagesWindow.ChangeColor else MessagesWindow.ChangeColor

        tf = self.plainTextEdit_MsgSent.currentCharFormat()
        tf.setForeground(self.__sentColor)
        self.plainTextEdit_MsgSent.setCurrentCharFormat(tf)
        
        self.plainTextEdit_MsgSent.appendPlainText(QDateTime.currentDateTime().toString("[yyyy-MM-dd HH:mm:ss:zzz]:") + sentMsg)

    def AddReceivedMsg(self, receivedMsg):
        self.__recvColor = MessagesWindow.DefaultColor if self.__recvColor == MessagesWindow.ChangeColor else MessagesWindow.ChangeColor

        tf = self.plainTextEdit_MsgReceived.currentCharFormat()
        tf.setForeground(self.__recvColor)
        self.plainTextEdit_MsgReceived.setCurrentCharFormat(tf)
        
        self.plainTextEdit_MsgReceived.appendPlainText(QDateTime.currentDateTime().toString("[yyyy-MM-dd HH:mm:ss:zzz]:") + receivedMsg)
        
    @Slot()
    def on_pushButton_ClearSent_clicked(self):
        self.plainTextEdit_MsgSent.clear()
        
    @Slot()
    def on_pushButton_ClearReceived_clicked(self):
        self.plainTextEdit_MsgReceived.clear()
        
    @Slot()
    def on_closeButton_clicked(self):
        self.reject()
